home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / gigo0714.zip / FXMUNGE.C < prev    next >
C/C++ Source or Header  |  1994-05-24  |  5KB  |  164 lines

  1. /*
  2.  
  3.     FX munge (FX Uucp), copyright (c) 1994 Jorge Cwik
  4.     munge uucp job's Unix filenames to DOS
  5.  
  6.     This routine munges filenames the same way as FX Uucp. Use this
  7.     code or equivalent if you want to interface with FX Uucp.
  8.  
  9.     LEGAL:
  10.     You are allowed to use this code verbatim or, in any modified
  11.     form for implementing software add-ons for FX Uucp. The copy-
  12.     right notice must be preserved in all cases.
  13.  
  14.     DISCLAIMER:
  15.     Even though we tested the program and tried to make it full
  16.     compatible with FX Uucp, this software is offered "as is".
  17.     We cannot guarantee that the code performs well and exactly
  18.     as intended. We also reserve the right to change the munging
  19.     algorythm in FX Uucp.
  20.  
  21.  
  22.     Unix filenames are of the form:
  23.         D.host<grade>jobId    (e.g.: D.laserC1234)
  24.  
  25.     There are a few variations on the above format. But in any
  26.     case it is not a valid DOS filename and must be munged.
  27.  
  28.     The munging algorythm must preserve the original uniqueness.
  29.     Because each host has its own spool directory, there is no
  30.     possibility of conflict between differents hosts. Thus, the
  31.     host name is not needed and discarded. However, two different
  32.     jobs may differ only in the case, the munging must still result
  33.     in different DOS filename (that doesn't preserve cases).
  34.  
  35.  
  36.     Detailed description:
  37.  
  38.     o Discard the prefix and the host name (the prefix will be used later)
  39.     o Take the last five letters and compute a bitmask to preserve the case
  40.     o Put the bitmask in the first letter of the munged filename
  41.     o Append up to seven of the last letters of the original Unix name
  42.     o Add the original prefix as the DOS filename extension.
  43.  
  44.     Discarding the host name:
  45.  
  46.     The routine must be passed the remote uucpname to be able to
  47.     separate it and discard it. Otherwise it is not possible to do
  48.     that. The length of the subfields are not fixed and the job ID
  49.     may be encoded in alphanumerical characters, not only digits.
  50.     Some systems use the remote uucpname (our local one) instead of
  51.     their own name. The code must check for this possibility. It
  52.     scans the Unix file name for the longer piece that matches _either_
  53.     the local or remote uucpname. This scan is done with case regarding
  54.     comparison.
  55.     
  56.     Sometimes the host name doesn't match anyone, neither the local
  57.     neither the remote. In this case, we just take the last seven
  58.     letters. This might happen, for example, if the uucpname we
  59.     are passed as parameter have the wrong case.
  60.  
  61.  
  62.     Computing the bitmask:
  63.  
  64.     Only the last five letters are used to compute the bitmask
  65.     Lowercase letters have a bit set, all the other characters a zero.
  66.     The rightmost (first) letter correspond to the less significant bit.
  67.     The whole byte is taken as a byte (whose range is 0-31) and enco-
  68.     ded in a single alphanumeric character. If is less than 10, use
  69.     digits 0-9, otherwise use uppercase letter.
  70.  
  71.  
  72.     This implementation takes the following parameter:
  73.  
  74.     char         *munged: pointer to a previouslly allocated buffer
  75.     const char    *unixname : the original Unix job's filename
  76.     const char    *remotname: the remot uucpname
  77.     const char    *localname: the local uucpname
  78.  
  79.     It returns 0 on ivalid input, 1 otherwise.
  80. */
  81.  
  82. #include <stdio.h>
  83. #include <string.h>
  84. #include <ctype.h>
  85.  
  86.  
  87. int fxMunge( char *munged, const char *unixname,
  88.     const char *remotname, const char *localname)
  89. {
  90.     const char *s, *s2;
  91.     unsigned bitmask;
  92.     int i;
  93.  
  94.     /* Simple sanity check, must be D. or X. */
  95.     if( unixname[1] != '.' || ( unixname[0] != 'D' && unixname[0] != 'X') )
  96.         return 0;
  97.  
  98.     /* Find the longest hostname match */
  99.     s2 = s = unixname + 2;
  100.     while( *s && *s == *remotname++)
  101.         s++;
  102.     while( *s2 && *s2 == *localname++)
  103.         s2++;
  104.     if( s2 > s)
  105.         s = s2;
  106.  
  107.  
  108.     /* 
  109.     Now 's' splits the Unix filename between the host name and
  110.     the rest, the jobID (plus grade) that we are interested.
  111.  
  112.     You may want to add other sanity checks based in the lenght
  113.     of the remaining string.
  114.     */
  115.  
  116.     s2 = s;        /* Remember pointer */
  117.  
  118.     /* Only the last five characters build the bitmask */
  119.     i = strlen(s) - 5;
  120.     if( i > 0)
  121.         s += i;
  122.  
  123.     for( bitmask = 0; *s; s++)
  124.     {
  125.         bitmask <<= 1;
  126.         if( islower( *s))
  127.             bitmask |= 1;
  128.     }
  129.  
  130.     /* You may use bitmask as an index in a table if you want */
  131.     /* Slightly faster and longer                             */
  132.  
  133.     bitmask += '0';
  134.     if( bitmask > '9')
  135.         bitmask += 'A' - '9' - 1;
  136.  
  137.     /* Take at most seven characters */
  138.     i = strlen(s2) - 7;
  139.     if( i > 0)
  140.         s2 += i;
  141.  
  142. #if 1
  143.     sprintf( munged, "%c%s.%c", bitmask, s2, unixname[0]);
  144. #else
  145.  
  146.     /* 
  147.     It may be overkill to use sprintf if it's never called in
  148.     any other sections of the program. This code is equivalent.
  149.     */
  150.  
  151.     {
  152.         char *ptr; /* We need a non-const pointer */
  153.  
  154.         munged[0] = bitmask;
  155.         ptr = stpcpy( munged + 1, s2);    /* similar to strcpy          */
  156.         *ptr++ = '.';                    /* but returns end of string */
  157.         *ptr++ = unixname[0];
  158.         *ptr++ = 0;
  159.     }
  160. #endif
  161.  
  162.     return 1;
  163. }
  164.